MID$ Function ---------------------------------------------------------------------------- Action Returns a substring of a string. Syntax MID$( stringexpression$, start%, length%) Remarks The MID$ function uses the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- stringexpression$ The string expression from which the substring is extracted. This can be any string expression. start% The character position in stringexpression$ where the Argument Description ---------------------------------------------------------------------------- stringexpression$ where the substring starts. length% The number of characters to extract. The arguments start% and length% must be between 1 and 32,767, inclusive. If length% is omitted or if there are fewer than length% characters in the string (including the start% character), the MID$ function returns all characters from the position start% to the end of the string. If start% is greater than the number of characters in stringexpression$, MID$ returns a null string. Use the LEN function to find the number of characters in stringexpression$. See Also LEFT$, LEN, MID$ Statement, RIGHT$ Example The following example converts a binary number to a decimal number. The program uses the MID$ function to extract digits from the binary number (input as a string). INPUT "Binary number = ", Binary$' Input binary number as string. Length = LEN(Binary$)' Get length of string. Decimal = 0 FOR K = 1 TO Length Digit$ = MID$(Binary$, K, 1) ' Get digit from string. IF Digit$ = "0" OR Digit$ = "1" THEN' Test for binary digit. Decimal = 2 * Decimal + VAL(Digit$)' Convert digits to numbers. ELSE PRINT "Error--invalid binary digit. "; Digit$ EXIT FOR END IF NEXT PRINT "Decimal number =" Decimal